/***
 * Copyright (c) 2021-2031 murenchao
 * fig is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *       http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package cool.taomu.software.fig.rpc.entity

import cool.taomu.software.fig.classloader.FigClassLoaderManage
import java.io.Serializable
import java.util.Vector
import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtend.lib.annotations.ToString
import cool.taomu.software.fig.utils.NTPUnixTimestamp
import org.eclipse.xtend.lib.annotations.EqualsHashCode

@Accessors
@ToString
class RpcResultEntity<T> {
    T result;
}

@Accessors
@ToString
@EqualsHashCode
class RpcEntity implements Serializable {

    String inter;
    String methodName;
    String returnType;
    String[] parameterTypes;
    Object[] arguments;
    long timestamp = new NTPUnixTimestamp().unixTimestamp;
    String valid;

    new(Class<?> inter, String methodName, Class<?> returnType, Class<?>[] parameterTypes, Object... arguments) {
        this.inter = inter.name;
        this.methodName = methodName;
        this.returnType = returnType.name;
        var ptypes = new Vector<String>();
        for (type : parameterTypes) {
            ptypes.add(type.name);
        }
        this.parameterTypes = newArrayOfSize(ptypes.size);
        this.parameterTypes = ptypes.toArray(this.parameterTypes);
        this.arguments = arguments
    }

    def getInter() {
        var fig = new FigClassLoaderManage();
        return fig.getClassLoader(this.inter);
    }

    def getReturnType() {
        var fig = new FigClassLoaderManage();
        return fig.getClassLoader(this.returnType);
    }

    def getParameterTypes() {
        var fig = new FigClassLoaderManage();
        var ptypes = new Vector<Class<?>>();
        for (type : this.parameterTypes) {
            ptypes.add(fig.getClassLoader(type));
        }
        var Class<?>[] parameterTypes = newArrayOfSize(ptypes.size);
        return ptypes.toArray(parameterTypes);
    }
}
